package com.example.smartcity_poverty.ui.AllService.Attorneys

import android.content.Intent
import android.graphics.Rect
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.model.Attreneys.AttBannerModel
import com.example.smartcity_poverty.logic.model.Attreneys.AttListModel
import com.example.smartcity_poverty.logic.model.Attreneys.AttMoreModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.Attorneys.activity.AttMe.AttMeActivity
import com.example.smartcity_poverty.ui.AllService.Attorneys.activity.AttShowListAllActivity
import com.example.smartcity_poverty.ui.AllService.Attorneys.adapter.AttListAdapter
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_attorneys.*
import kotlinx.android.synthetic.main.re_att_more.*
import kotlinx.coroutines.Dispatchers

/**
 * 律师咨询主页功能模块描述.
 */
class AttorneysActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_attorneys)
        title = "律师咨询主页"

        // 开启协程
        run()

        attMe.setOnClickListener {
            startActivity(Intent(this,AttMeActivity::class.java))
        }

        // 点击更多进入律师列表页
        attHomeAll.setOnClickListener {
            startActivity(Intent(this, AttShowListAllActivity::class.java).putExtra("id", 0))
        }

        // 点击进入我的律师咨询
        attHomeBtn.setOnClickListener {
            startActivity(Intent(this, AttMeActivity::class.java))
        }
    }

    /**
     * 管理所有挂起函数
     */
    private fun run() = coroutine(Dispatchers.IO) {
        // 律师咨询主页轮播图
        setBanner()

        // 律师专长
        setMore()

        // 本月上榜优选律师
        setList()
    }


    /**
     * 律师咨询主页轮播图
     */
    private suspend fun setBanner() {
        // 判断是否已经登录!
        if (getToken().isNotEmpty()) {
            // 获取轮播图数据
            api.getAttBanner(getToken()).apply {
                if (this.code == 200) {
                    val data = this.data
                    runOnUiThread {
                        // 配置轮播图适配器
                        attBanner.apply {
                            this.addBannerLifecycleObserver(this@AttorneysActivity).indicator =
                                CircleIndicator(context)
                            this.setBannerRound(25f)
                            this.adapter =
                                object : BannerImageAdapter<AttBannerModel.DataBean>(data) {
                                    override fun onBindView(
                                        p0: BannerImageHolder?,
                                        p1: AttBannerModel.DataBean?,
                                        p2: Int,
                                        p3: Int
                                    ) {
                                        // 网络加载图片
                                        glide(url + p1?.imgUrl, p0?.imageView!!)
                                    }

                                }
                        }
                    }
                } else runOnUiThread { this.msg?.show() }

            }
        } else "请先登录！".show()
    }


    /**
     * 律师专长
     */
    private suspend fun setMore() {
        // 判断是否·已经登录
        if (getToken().isNotEmpty()) {
            api.getAttMoreAll(getToken()).apply {
                if (this.code == 200) {
                    runOnUiThread {
                        // 填充数据
                        val rows = this.rows

                        // 适配器
                        attMoreRv.apply {
                            this.isNestedScrollingEnabled = false
                            this.layoutManager = GridLayoutManager(context, 2).apply {
                                this.orientation = GridLayoutManager.HORIZONTAL
                            }
                            this.adapter = AttMoreAdapter(ArrayList<AttMoreModel.RowsBean>().apply {
                                this.addAll(rows!!)
                            }, R.layout.re_att_more)
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        } else "请先登录！".show()
    }


    /**
     * 本月上榜优选律师
     */
    private suspend fun setList() {
        if (getToken().isNotEmpty()) {
            val d = api.getAttListAll(getToken())
            runOnUiThread {
                if (d.code == 200) {
                    // 填充数据
                    attListRv.apply {
                        this.isNestedScrollingEnabled = false
                        this.layoutManager = LinearLayoutManager(context)
                        this.adapter = AttListAdapter(ArrayList<AttListModel.DataBean>().apply {
                            this.addAll(d.data!!)
                        }, R.layout.re_att_home_list_all)
                    }
                } else d.msg?.show()
            }
        } else "请先登录！".show()
    }


    /**
     * 顶部按钮返回
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }

    /**
     * 适配器
     */
    companion object {
        /**
         * 法律专长列表适配器
         */
        class AttMoreAdapter(
            val list: MutableList<AttMoreModel.RowsBean>,
            layoutId: Int
        ) : BaseRecyclerAdapter<AttMoreModel.RowsBean>(list, layoutId) {
            override fun setData(
                view: BaseViewHolder.ViewFind,
                data: AttMoreModel.RowsBean,
                position: Int,
                holder: BaseViewHolder
            ) {
                // 排序
                list.sortByDescending { it.id }

                // 填充数据
                val d = list[position]
                val fv = holder.viewFind
                val con = holder.viewFind.view?.context

                glide(url + d.imgUrl, fv.attMoreImage)
                fv.attMoreTitle.text = d.name

                holder.itemView.setOnClickListener {
                    con?.startActivity(
                        Intent(
                            con,
                            AttShowListAllActivity::class.java
                        ).putExtra("id", d.id.toString())
                    )
                }
            }
        }
    }
}